package cn.itcast.order.service;

import cn.itcast.order.mapper.OrderMapper;
import cn.itcast.order.pojo.Order;

import cn.itcast.order.pojo.User;
import cn.itcast.order.feign.UserFeignClient;
import com.alibaba.fastjson.JSON;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


@Service
public class OrderService {

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private UserFeignClient userFeignClient;

    public String queryOrderById(Long orderId) {
        // 1.查询订单
        Order order = orderMapper.findById(orderId);
        //2.远程调用用户微服务http://localhost:8081/order/1  返回User对象
        //User order = restTemplate.getForObject("http://localhost:8081/user/"+order.getUserId(),User.class);
        // User order = restTemplate.getForObject("http://userservice/user/"+order.getUserId(),User.class);
        User user = userFeignClient.findById(order.getUserId());
        //3.把用户user对象set到order对象中
        order.setUser(user);
        // 4.返回
        return JSON.toJSONString(order);
    }

  /*  //当前降级方法要和原方法参数/返回值类型保持一直
    public String queryOrderByIdFallback(Long orderId) {
        return "服务器太忙，请稍后重试.....";
    }

    //全局降级
    public String allMethodFallBack(){
        return " 服务器太太太忙，请稍后重试....";
    }*/
}
